<template>
    <el-dialog v-model="dialogVisible" width="50%" title="新增用户" @close="closeWin" destroy-on-close center>
        <template #default>
            <el-form :model="newUser" label-width="auto" >
                <el-form-item label="头像">
                    <el-upload
                        class="avatar-uploader"
                        :action="uploadUrl"
                        name="file"
                        :auto-upload="true"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload"
                    >
                        <!-- 回显图片的 -->
                        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item label="登录名">
                    <el-input v-model="newUser.username" />
                </el-form-item>
                <el-form-item label="昵称">
                    <el-input v-model="newUser.nickname" />
                </el-form-item>
                <el-form-item label="登陆密码">
                    <el-input v-model="newUser.loginPass" />
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="newUser.userTel" />
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="newUser.userEmail" />
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="newUser.userGender" class="ml-4">
                        <el-radio :value="1" >男</el-radio>
                        <el-radio :value="2" >女</el-radio>
                        <el-radio :value="99" >未知</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" :rows="2" v-model="newUser.notes" />
                </el-form-item>
            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="20"><el-button type="primary" @click="doSubmit">提交</el-button></el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="closeWin">关闭</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>

<script>
import {watch} from "vue";
import UserRequest from '../../../api/user.js';

export default {
    props:["visible"],// 属性
    emits:["optionCompleted"],// 暴漏的事件
    name: "user-add-win.vue",
    data(){
        return{
            dialogVisible: false, // 弹窗标记
            newUser:{deptId:12},// 保存新用户对象
            imageUrl:undefined,// 上传成功之后头像的访问地址
            uploadUrl:"http://localhost:8080/sys/upload/image",// 上传文件的地址
        }
    },methods:{
        /**
         * 上传成功的处理
         * @param response 响应的结果
         * @param file 上传的文件对象
         */
        handleAvatarSuccess(response, file, fileList){
            // 将图片访问地址设置到imageUrl中
            this.imageUrl = response.data;
        },
        /**
         * 上传头像之前的勾子函数
         * @param file 要上传的文件
         */
        beforeAvatarUpload(file){
            // 验证图片大小和格式
            //return false;// 阻止上传
        },
        /**
         * 提交按钮的事件
         */
        doSubmit(){
            // 设置用户头像
            this.newUser.userHead = this.imageUrl;
            //  提交
            UserRequest.saveUser(this.newUser,res=>{
                if(res.code == '200'){
                    // 提示成功
                    this.$message({
                        type: 'success',
                        message: '新增用户成功!'
                    });
                    // 通知父窗体，操作完成
                    this.$emit("optionCompleted");
                }else{
                    this.$message({
                        type: 'error',
                        message: '新增用户失败!'
                    });
                }
            });
        },
        /**
         * 关闭当前的窗体
         */
        closeWin(){
            // 通知父窗体，操作完成
            this.$emit("optionCompleted");
        }
    },mounted(props) {
        //this.loadDictItems();
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        })
    }
}
</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
 .avatar-uploader .el-upload {
     border: 1px dashed var(--el-border-color);
     border-radius: 6px;
     cursor: pointer;
     position: relative;
     overflow: hidden;
     transition: var(--el-transition-duration-fast);
 }

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>